package com.project.restaurant.user.aop;


import com.project.restaurant.common.api.ResponseResult;
import com.project.restaurant.common.api.ServiceStateCode;
import com.project.restaurant.common.key.SessionAttribute;
import com.project.restaurant.pojo.User;
import com.project.restaurant.user.vo.UserPublicInfoVo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;

@Component
@Aspect
public class LoginAop {

    @Pointcut("execution(* com.project.restaurant.user.controller.LoginController.login(..))")
    public void pointcut(){
    }

    @Around("pointcut()")
    public Object aroundNotice(ProceedingJoinPoint joinPoint){
        Object returnValue = null;
        try {
//            //获取切面方法的所有参数
//            Object[] args = joinPoint.getArgs();
//            boolean findState= false;
//            for(Object obj:args){
//                if(obj instanceof HttpSession){
//                    HttpSession session = (HttpSession) obj;
//                    User nowLoginUser = (User) session.getAttribute(SessionAttribute.NOW_LOGIN_USER);
//                    if(nowLoginUser!=null){
//                        return ResponseResult.auto(ServiceStateCode.SUCCESS, UserPublicInfoVo.getUserSafeInfoVo(nowLoginUser));
//                    }
//                    findState = true;
//                    break;
//                }
//            }
//            if(!findState){
//                throw new Exception("登录方法参数中不存在session对象");
//            }
            returnValue = joinPoint.proceed();
            //登录后的预处理
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return returnValue;
    }
}
